from pyquery import PyQuery as pq
from urlparse import urlsplit

def rewrite_links(content, theme, resource_fetcher, log, app_id):
    """
    Fix links from dspace
    """
    if app_id == "dspace":
        replace_link_values = {'/xmlui/': '/dspace/xmlui/'}
    elif app_id == "cynin":
        replace_link_values = {'5000': '5000/cynin'}
         
    for link_value in replace_link_values:
        theme_value = theme('head').html()
        if link_value in theme('head').html():
            theme('head').replaceWith(theme_value.replace(link_value, replace_link_values[link_value]))
        theme_value = theme('body').html()
        if link_value in theme('body').html():
            theme('body').replaceWith(theme_value.replace(link_value, replace_link_values[link_value]))         


   
